package com.metrohm.edu.validation.sample2.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.metrohm.edu.validation.sample2.DosingUnit;

public class DosingRateValidator implements ConstraintValidator<ValidDosingRate, DosingUnit> {

    @Override
    public void initialize(ValidDosingRate constraintAnnotation) {
    }

    @Override
    public boolean isValid(DosingUnit value, ConstraintValidatorContext context) {
        int cylVol = value.getCylinderVolume();
        if ( cylVol > 0) {
            int max = (int)(cylVol * 60.0 / 18.0);
            return value.getSpeed() <= max;
        } else {
            return false;
        }
    }

}
